package com.example.superoa.model.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.DepartmentDao;
import com.example.superoa.model.dao.JobchageDao;
import com.example.superoa.model.dao.PositionDao;
import com.example.superoa.model.dao.TransferApprovalDao;
import com.example.superoa.model.dao.TransferDao;
import com.example.superoa.model.dao.TransferJlDao;
import com.example.superoa.model.dao.TransferTypeDao;
import com.example.superoa.model.mappers.TransferMapper;
import com.example.superoa.model.pojos.Department;
import com.example.superoa.model.pojos.Jobchage;
import com.example.superoa.model.pojos.Position;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.pojos.Transfer;
import com.example.superoa.model.pojos.TransferApproval;
import com.example.superoa.model.pojos.TransferJl;
import com.example.superoa.model.pojos.TransferType;
import com.example.superoa.model.vo.TransferVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class TransferService {
	@Autowired
	private TransferMapper trmapper;
	@Autowired
	private TransferDao transferdao;
	@Autowired
	private TransferJlDao tjldao;
	@Autowired
	private DepartmentDao d_dao;
	
	@Autowired
	private PositionDao p_dao;
	
	@Autowired
	private JobchageDao j_dao;
	
	@Autowired
	private TransferTypeDao typesdao;
	@Autowired
	private TransferApprovalDao tapprovaldao;
	
	Shijian sj=new Shijian();
	
	
	public TransferVo transferName(int staff_id) {
		return trmapper.transferName(staff_id);
		
	}
	
	
	//调动类型
	public void addtransfertype(TransferType type,int id,Integer transfertypesId) {
		//生成编号
		//transfertypesId=addtypesid();
		type.setTransfer(transferdao.findById(id).get());
		typesdao.save(type);
	}
	//调动审批表
	public void addTransferApproval(TransferApproval tl,int id) {
		tl.setTransfer(transferdao.findById(id).get());
		tl.setApproval_state("未审核");
		tl.setApproval_time(new Date(sj.getNetworkTime()));
		tapprovaldao.save(tl);
	}
	//新增调动记录表
	public void addTransferJl(int jlid,int jlidd,int departNamea,int position_name,TransferJl tjl,int id) {
		//当jlidd为0时为调动后 ，为主数据id时为调动前数据
				tjl.setTransferjl_id(jlid);
				tjl.setDepartment(d_dao.findById(departNamea).get());
				tjl.setPosition(p_dao.findById(position_name).get());
				tjl.setTransfer(transferdao.findById(id).get());
				tjl.setTransferjl_iid(jlidd);
				tjldao.save(tjl);
	}
	//新增调动表
	public void addtransfer(int id,Transfer t,Staff s,Integer transfer_id) {
		transfer_id=addsid();
		t.setTransfer_id(transfer_id);
		t.setStaff(s);
		t.setTransferState("未确认");
		transferdao.save(t);
	}
	//调动表id
	public Integer selectIID() {
		if(trmapper.selectIID()==null) {
			return 1;
		}
		return trmapper.selectIID()+1;
	}
	public Integer selectjlIID(){
		if(trmapper.selectjlIID()==null) {
			return 1;
		}
		return trmapper.selectjlIID()+1;
	}
	
	//查询调动前后数据
	public JSONArray selectransfer(){
		return trmapper.selectransfer();
	}
	//查询调动结果
	public JSONArray selectransferjg() {
		return trmapper.selectransferjg();
	}
	//审核状态
	public void transferstate(String approval_state,int approval_id) {
		System.out.println("contreact_id=="+approval_id);
		System.out.println("Contreact_qdstate=="+approval_state);
		
		trmapper.transferstate(approval_state, approval_id);
	}
	//生成编号
	public List<Transfer> transferALId(){
		return trmapper.transferALId();
	}
	//自增编号
	public int addsid(){
		int sid=0;
		List<Transfer> list=trmapper.transferALId();
		for(Transfer exat : list) {
			if(sid<exat.getTransfer_id()) {
				sid=exat.getTransfer_id();
			}
		}
		return sid+1;
	}
	
	//根据ID查询数据
	public TransferVo selectransferById(int Approval_id) {
		return trmapper.selectransferById(Approval_id);
	}
	//类型编号
	/*public int addtypesid(){
		int sid=0;
		List<TransferType> list=trmapper.transferTypeId();
		for(TransferType exat : list) {
			if(sid<exat.getTransfertypesId()) {
				sid=exat.getTransfertypesId();
			}
		}
		return sid+1;
	}*/
}
